<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="@/assets/logo.png" alt="" />
        <span>Vue管理系统</span>
      </div>
      <el-button type="info" @click="goBack">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 侧边栏菜单区域 -->
        <!-- active-text-color 当前活动窗体的颜色 -->
        <!-- router 开启路由模式 -->
        <el-menu
          background-color="#373D41"
          text-color="#fff"
          active-text-color="#499CFD"
          unique-opened
          router
          :collapse="isCollapse"
          :collapse-transition="false"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsList[item.id]" style="margin-right: 15px"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <!-- 二级菜单模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
        <div class="toggle-button" @click="toggleCollapse">|||</div>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      // 导航栏数据
      menuList: [],
      iconsList: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-more',
        101: 'iconfont icon-location-shopping',
        102: 'iconfont icon-file-text',
        145: 'iconfont icon-graph'
      },
      // 默认折叠关闭
      isCollapse: false,
      // 被激活链接的空地址
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    goBack() {
      // 推出后清空window.sessionStorage的token值
      window.sessionStorage.clear()
      // 回退到login
      this.$router.push('/login')
    },
    // 获取导航栏数据
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取导航栏数据失败！')
      }

      this.menuList = res.data
      // console.log(this.menuList)
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100vh;
}
.el-header,
.el-aside {
  background-color: #373d41;
}
.el-header {
  display: flex;
  justify-content: space-between;
  padding: 0;
  align-items: center;
  color: #fff;
  font-size: 25px;
  > div {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  img {
    height: 60px;
  }
}
.el-menu {
  border-right: 0;
  transition: all 0.1s;
}
.iconfont {
  margin-right: 10px;
  margin-left: 5px;
}
.el-aside {
  position: relative;
}
.toggle-button {
  position: absolute;
  bottom: 0;
  width: 100%;
  line-height: 30px;
  text-align: center;
  cursor: pointer;
  background: #566066;
  color: #fff;
}
</style>
